package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll() {

        // JDBC 查询数据库
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        List<User> list = null;//创建容器
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://192.168.58.129:3306/JDBC", "root", "itheima");
            //
            stat = conn.createStatement();
            String sql = "select * from user";
            rs = stat.executeQuery(sql);
            // 处理结果集
            list = new ArrayList<>(); // 定义容器封装查询用户集合
            while (rs.next()) { // 游标向下移动一行
                // 获取每一个字段的值
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");
                // 创建一个User对象, 封装每一行数据
                User user = new User(id, name, gender, age, address, qq, email, "", "");
                // 添加到集合中
                list.add(user);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (stat != null) {
                    stat.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;

    }

    @Override
    public void save(User users) {
        Connection conn = null;
        Statement stat = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://192.168.58.129:3306/JDBC", "root", "itheima");
            stat = conn.createStatement();
            String sql = "insert into user values(null,'" + users.getName() + "','" + users.getGender() + "'," + users.getAge() + ",'" + users.getAddress() + "','" + users.getQq() + "','" + users.getEmail() + "',null,null)";
            stat.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stat != null) {
                    stat.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    public void delete(String uid) {
        Connection con = null;
        Statement st = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://192.168.58.129:3306/JDBC", "root", "itheima");
            st = con.createStatement();
            String sql = "delete from user where id=" + uid;
            st.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public User findById(String uid) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://192.168.58.129:3306/JDBC", "root", "itheima");
            st = con.createStatement();
            String sql = "select * from user where  id=" + uid;
            rs = st.executeQuery(sql);
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");
                // 创建一个User对象, 封装每一行数据
                User user = new User(id, name, gender, age, address, qq, email, "", "");
                return user;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs!=null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (st!=null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con!=null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    public void update(User user) {
        Connection con = null;
        Statement st = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://192.168.58.129:3306/JDBC", "root", "itheima");
            st = con.createStatement();
            String sql = "update user set name = '" + user.getName() + "',gender = '" + user.getGender() + "' ,age = " + user.getAge() + " , address = '" + user.getAddress() + "' , qq = '" + user.getQq() + "', email = '" + user.getEmail() + "' where id = " + user.getId();
            st.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st!=null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con!=null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
